package quizman.mappers;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import quizman.domain.MultipleChoiceQuestion;
import quizman.domain.NumericQuestion;
import quizman.domain.Question;
import quizman.domain.Quiz;
import quizman.domain.ShortAnswerQuestion;
import quizman.tdg.QuestionTDG;

/**
 * 
 */

/**
 * @author Team0i
 *
 */
public class QuestionMapper {
	public static List<Question> getQuestions(String quizName) throws Exception {
		try {
			ResultSet rs = QuestionTDG.find(quizName);
			List <Question> questionList = new ArrayList<Question>();
			if(!rs.next()) return null;
			do {
				String questionName = rs.getString("Question_Name");
				String questionText = rs.getString("Question_Text");
				int question_Type = rs.getInt("Question_Type");
				Question q;
				switch(question_Type){
					case 1:
						q = new MultipleChoiceQuestion(questionName, questionText);
						((MultipleChoiceQuestion) q).setChoices(MultipleChoiceMapper.find(questionName));
						break;
					case 2:
						q = new ShortAnswerQuestion(questionName, questionText);
						((ShortAnswerQuestion) q).setAnswer(ShortAnswerMapper.find(questionName));
						break;
					case 3:
						q = new NumericQuestion(questionName, questionText);
						((NumericQuestion) q).setLowerBound(NumericAnswerMapper.find(questionName,1));
						((NumericQuestion) q).setUpperBound(NumericAnswerMapper.find(questionName,2));
						break;
					default:
						q = null;
				}
				questionList.add(q);
			} while(rs.next());
			return questionList;
		} catch (Exception e) {
			String msg = "Error in QuestionMapper.find";
			throw new Exception(msg, e);
		}
	}
	public static Question getQuestion(String quizName, String questionName) throws Exception {
		try {
			ResultSet rs = QuestionTDG.find(quizName, questionName);
			Question q;
			if(!rs.next()) return null;
				String qName = rs.getString("Question_Name");
				String questionText = rs.getString("Question_Text");
				int question_Type = rs.getInt("Question_Type");

				switch(question_Type){
					case 1:
						q = new MultipleChoiceQuestion(qName, questionText);
						((MultipleChoiceQuestion) q).setChoices(MultipleChoiceMapper.find(questionName));
						break;
					case 2:
						q = new ShortAnswerQuestion(qName, questionText);
						((ShortAnswerQuestion) q).setAnswer(ShortAnswerMapper.find(questionName));
						break;
					case 3:
						q = new NumericQuestion(questionName, questionText);
						((NumericQuestion) q).setLowerBound(NumericAnswerMapper.find(questionName,1));
						((NumericQuestion) q).setUpperBound(NumericAnswerMapper.find(questionName,2));
						break;
					default:
						q = null;
				}
			return q;
		} catch (Exception e) {
			String msg = "Error in QuestionMapper.find";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(Quiz quiz, Question q) throws Exception {
		try {
			String qClass = q.getClass().toString();
			int type;
			if(qClass.contains("MultipleChoiceQuestion")) type = 1;
			else if(qClass.contains("ShortAnswerQuestion")) type = 2;
			else if(qClass.contains("NumericQuestion")) type = 3;
			else type = 0;
			QuestionTDG.insert(q.getName(), q.getText(), quiz.getName(), type);
		} catch (Exception e) {
			String msg = "Error in QuestionMapper.insert";
			throw new Exception(msg,e);
		}
	}
	
	public static void update(Question q) throws Exception {
		try {
			QuestionTDG.update(q.getName(), q.getText());
		} catch (Exception e) {
			String msg = "Error in QuestionMapper.update";
			throw new Exception(msg,e);
		}
	}
	
	public static void delete(Question q) throws Exception {
		try {
			QuestionTDG.delete(q.getName());
		} catch (Exception e) {
			String msg = "Error in QuizMapper.delete";
			throw new Exception(msg,e);
		}
	}
}
